var express = require('express');
var router = express.Router();

var MongoClient = require("./../mongodb/MongoClient");
var MySql = require("./../mongodb/MySql");

var fs = require("fs");
var multer = require("multer");
var upload = multer({
  dest:"public/uploads"
})

router.post("/img-upload",upload.single("myimg"),(req,res) => {
  console.log(req.file)
  console.log(req.body)
fs.rename(req.file.destination + "/" + req.file.filename, req.file.destination + "/" + req.body.user + ".png" , () => {
    //图片存入数据库
    MongoClient.mongoConnect("pull", (db) => {
    	console.log("ok")
      MySql.updateData(db, "adduser", {username:req.body.user}, {$set:{img:"http://localhost:3000/uploads/" + req.body.user + ".png"}},function(){
        res.send("1")
        db.close();
      })
    }, (err) => {
      console.log(err)
       db.close();
    })
})
})

module.exports = router;
